package gw.com.android.ui.positions;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import gw.com.android.model.DataManager;
import gw.com.android.utils.ServerConnnectUtil;
import www.com.library.app.PushMsgTabFragment;
import www.com.library.util.NetworkMonitor;
import www.com.library.view.EfficientRecyclerView;
import www.com.library.view.LoadingProgress;

/**
 * Created by reeta.zou on 2016/7/7.
 */
public abstract class SubFragment extends PushMsgTabFragment {

    /**
     * 数据加载圈
     */
    @BindView(R.id.loading_progress_view)
    protected LoadingProgress mProgress;
    @BindView(R.id.empty_layout)
    protected View mEmptyLayout;
    @BindView(R.id.empty_title)
    protected TextView mEmptyView;
    @BindView(R.id.create_order)
    protected TextView mCreateOrder;
    /**
     * 持仓列表控件
     **/
    @BindView(R.id.recycler_view)
    protected EfficientRecyclerView mListView;
    protected RecylerListAdapter mAdapter;
    protected String mCurrentTag;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return mRootView;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_trade_list;
    }

    @Override
    protected void initLayoutView() {
        ButterKnife.bind(this, mRootView);
        // 抢占点击事件，以便下层界面不可点击
        mProgress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        setEmptyView();
        mEmptyLayout.setVisibility(View.GONE);
        // 线性布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        // 设置布局管理器
        mListView.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshViewData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        refreshViewData();
    }

    /**
     * 持仓列表是否有数据判断后显示对应的视图
     */
    protected void refreshViewData() {
        if (null == mAdapter || null == mProgress || null == mListView) {
            return;
        }
        if (!mListView.mIsOnTouch && !mListView.mIsScrolling) {
            mAdapter.refreshData();
        }
        if (DataManager.instance().getLoadDataState(mCurrentTag)) {
            mProgress.setVisibility(View.GONE);
            if (mAdapter.getItemCount() < 1) {
                mListView.setVisibility(View.GONE);
                mEmptyLayout.setVisibility(View.VISIBLE);
            } else {
                mListView.setVisibility(View.VISIBLE);
                mEmptyLayout.setVisibility(View.GONE);
            }
        } else {
            mListView.setVisibility(View.GONE);
            mEmptyLayout.setVisibility(View.GONE);
            if (NetworkMonitor.hasNetWork() && !ServerConnnectUtil.instance().isReconnect) {
                mProgress.setVisibility(View.VISIBLE);
            } else {
                mProgress.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 显示隐藏加载圈
     */
    public void onProgressVisible(boolean isVisible) {
        if (null != mProgress) {
            if (isVisible) {
                if (DataManager.instance().getLoadDataState(mCurrentTag)) {
                    mProgress.setVisibility(View.GONE);
                } else {
                    mProgress.setVisibility(View.VISIBLE);
                }
            } else {
                mProgress.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 报价来了刷新界面
     */
    public void refreshPrice(int codeId) {
        if (isResumed() && !isHidden()) {
            if (null != mListView && !mListView.mIsOnTouch && !mListView.mIsScrolling) {
                if (null != mAdapter) {
                    mAdapter.refreshPrice(codeId);
                }
            }
        }
    }

    /**
     * 单个订单更新界面
     */
    public void refreshOrder(int orderId) {
        if (isResumed() && !isHidden()) {
            if (null != mListView && !mListView.mIsOnTouch && !mListView.mIsScrolling) {
                if (null != mAdapter) {
                    mAdapter.refreshOrder(orderId);
                }
            }
        }
    }

    public abstract void setEmptyView();

}
